<?php
/*
 * SDOSelfCheckServer
 * 依次调用每个频道的自检程序
 */

class SDOSelfCheckServer
{
    static protected $query = "/SelfCheck.php";
    static protected $filePath = "selfcheck.log";
    static protected $sig   = "147896325";
    
    static public function run()
    {
        $data = array();
        $servers = require dirname(__FILE__) . "/../config/SelfCheckServerConfig.php";
        foreach ($servers as $ip => $items)
        {
                $item = implode('|', $items['item']);
                
                $param = array(
                    "item"  => $item,
                    "sig"   => md5(self::$sig),
                    "time"  => time()
                );
                
                $req_url = "http://" . $ip . self::$query . "?" . http_build_query($param);
                $head = "Host: " . $items['channel'];
                
                echo "Query: " . $req_url . "\r\n"; 
                
                $result = self::comm_curl_request($req_url, "", array(CURLOPT_HTTPHEADER => array($head)));
                if (false === $result)
                {
                    $data[$ip] = array(
                        'stat' =>  1,
                        'msg'  =>  "no response host: " . $ip,
                        'timestamp' => time(),
                    );
                }
                
                $result = json_decode($result, true);
                if (empty($result))
                {
                    //continue?
                    continue;
                }
                
                foreach ($result as $key => $value)
                {
                    $value['ip'] = $ip;
                    
                    $data[$key.".".$ip] = $value;
                }    
        }
        
        $fp = fopen(self::$filePath, "w");
        
        fprintf($fp, "%s", json_encode($data));
        
        fclose($fp);
        
        return;
    }
    
    static public function get()
    {
        $fp = fopen(self::$filePath, "r");
        if (!$fp)
        {
            return "can not open log file";
        }
        $file_size = filesize(self::$filePath);
        
        $buffer_size = 1024;
        $cur_pos = 0;
        
        $result = "";
        
        while(!feof($fp)&& $file_size != $cur_pos)
        {
            $buffer = fread($fp,$buffer_size);
            if (false === $buffer)
            {
                break;
            }
            $len = strlen($buffer);
            $cur_pos += $len;
            $result .= $buffer; 
        }
        fclose($fp);
        

        return $result;
    }
    
    
    //string url 请求网址
    //$postString  要post的参数 格式如  xxx=1&yyy=2
    //$option 参数 如 array(CURLOPT_REFERER=>'http://www.51.com/',CURLOPT_HTTPHEADER=>array('Host: client.51.com'))
    //$req_url   = "http://114.80.88.200/client.php?member_name={$member_name}&key={$key}";
    //$cp_result    = comm_curl_request($req_url,'',array(CURLOPT_HTTPHEADER=>array('Host: rz.51.com'))); 
    static protected function comm_curl_request($url,$postString='',$option='')
    {
        $ch = curl_init();
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$postString);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch,CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
        curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5);
        
        //设置其他参数
        if(!empty($option) && is_array($option))
        foreach($option as $key => $val)
        {
            curl_setopt($ch, $key,$val);
        }

        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }
}